﻿//
// <author>David Nohejl</author>
//
// Contains menu handlers and logic for inserted items feature.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using Adorners_ResizingAdorner;
using System.Windows.Media.Imaging;

namespace WPFTrees
{
    //
    // Metody pro praci s objekty vkladanymi na pracovni plochu.
    //
    public partial class MainWindow
    {
        private void miInsertPicture(object sender, RoutedEventArgs e)
        {
            // Create the image element.
            Image simpleImage = new Image();
            simpleImage.Width = 50;
            simpleImage.Margin = new Thickness(0);

            // Create source.
            BitmapImage bi = new BitmapImage();            
            // BitmapImage.UriSource must be in a BeginInit/EndInit block.
            bi.BeginInit();
            bi.UriSource = new Uri(@"/images/empty-picture.bmp", UriKind.RelativeOrAbsolute);
            bi.EndInit();
            // Set the image source.
            simpleImage.Source = bi;            
            // add context menu            
            simpleImage.ContextMenu = CreateContextMenu(simpleImage);
            
            parentCanvas.Children.Add(simpleImage);

            // add resizing adorner

            AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(parentCanvas);
            adorner = new ResizingAdorner(simpleImage);

            adornerLayer.Add(adorner);

            RegisterInsertedObjectHandlers(simpleImage);
        }

        void RegisterInsertedObjectHandlers(FrameworkElement insertedObject)
        {
            insertedObject.MouseLeftButtonDown += new MouseButtonEventHandler(insertedElement_MouseLeftButtonDown);
            insertedObject.MouseMove += new MouseEventHandler(insertedElement_MouseMove);
            insertedObject.MouseLeftButtonUp += new MouseButtonEventHandler(insertedElement_MouseLeftButtonUp);
            insertedObject.MouseEnter += new MouseEventHandler(insertedObject_MouseEnter);
            insertedObject.MouseLeave += new MouseEventHandler(insertedObject_MouseLeave);
        }

        void insertedObject_MouseLeave(object sender, MouseEventArgs e)
        {
            FrameworkElement insertedObject = sender as FrameworkElement;
            statusBar1.Items[0] = "";
        }

        void insertedObject_MouseEnter(object sender, MouseEventArgs e)
        {
            FrameworkElement insertedObject = sender as FrameworkElement;
            statusBar1.Items[0] = "Right click to perform action on this object.";
        }

        ContextMenu CreateContextMenu(UIElement item)
        {
            ContextMenu menu = new System.Windows.Controls.ContextMenu();
            MenuItem mi = new MenuItem();
            mi.Header = "Properties...";
            mi.Tag = item;
            mi.Click += new RoutedEventHandler(mi_InsertedElementProperties);
            menu.Items.Add(mi);
            mi = new MenuItem();
            mi.Header = "Delete";
            mi.Tag = item;
            mi.Click += new RoutedEventHandler(miDeleteInsertedElement_Click);
            menu.Items.Add(mi);

            return menu;
        }

        private void miInsertText(object sender, RoutedEventArgs e)
        {
            // Create the image element.
            TextBlock text = new TextBlock();
            text.Text = "text here...";

            // add context menu

            text.ContextMenu = CreateContextMenu(text);

            parentCanvas.Children.Add(text);

            // add resizing adorner

            AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(parentCanvas);
            adorner = new ResizingAdorner(text);

            adornerLayer.Add(adorner);
            
            RegisterInsertedObjectHandlers(text);
        }

        void mi_InsertedElementProperties(object sender, RoutedEventArgs e)
        {
            MenuItem mi = sender as MenuItem;
            UIElement elem = mi.Tag as UIElement;
            TextBlock elemText = elem as TextBlock;
            Image elemImg = elem as Image;
            if (elemText != null)
            {
                PropertyDialogs.TextPropertyDlg dlg = new PropertyDialogs.TextPropertyDlg();
                dlg.Text = elemText.Text;
                if (dlg.ShowDialog() == true)
                {
                    elemText.Text = dlg.Text;
                }
            }
            else if (elemImg != null)
            {
                PropertyDialogs.ImagePropertyDlg dlg = new PropertyDialogs.ImagePropertyDlg();
                dlg.Image = elemImg.Source;
                if (dlg.ShowDialog() == true)
                {
                    elemImg.Source = dlg.Image;
                }
            }
        }

        void miDeleteInsertedElement_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = sender as MenuItem;
            UIElement elem = mi.Tag as UIElement;
            parentCanvas.Children.Remove(elem);
        }

        private void insertedElement_MouseMove(object sender, MouseEventArgs e)
        {
            UIElement elem = sender as UIElement;

            if (elem.IsMouseCaptured)
            {

                Mouse.GetIntermediatePoints(elem, points);
                double deltaX = (points[0].X - points[1].X);
                double deltaY = (points[0].Y - points[1].Y);

                if (Math.Abs(deltaX) >= maxDelta)
                    deltaX = maxDelta * Math.Sign(deltaX);

                if (Math.Abs(deltaY) >= maxDelta)
                    deltaY = maxDelta * deltaY == 0 ? 0 : Math.Sign(deltaY);

                Point pt = Mouse.GetPosition(parentCanvas);
                Point pt2 = parentCanvas.TranslatePoint(pt, elem);
                Canvas.SetLeft(elem, pt.X - mousePosInCanvas.X);
                Canvas.SetTop(elem, pt.Y - mousePosInCanvas.Y);
            }            
        }

        private void insertedElement_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            UIElement elem = sender as UIElement;
            if (!elem.IsMouseCaptured)
            {
                mousePosInCanvas = Mouse.GetPosition(elem);
                elem.CaptureMouse();
            }
            this.Cursor = Cursors.Hand;
        }

        private void insertedElement_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            UIElement elem = sender as UIElement;
            elem.ReleaseMouseCapture();
            this.Cursor = Cursors.Arrow;
        }
    }
}
